package com.my.blog.website;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Properties;

/**
 * <p>Title         :<br/></p>
 * <p>饿汉式   :<br/></p>
 * <p>
 * Created by run on 2018/1/15.
 */
public class PropertiesUtils2 {
    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtils2.class);

    private static Properties props = null;
    private static PropertiesUtils2 instance = new PropertiesUtils2();
    private PropertiesUtils2(){
        loadProps();
    }

    public static PropertiesUtils2 getInstance(){
        return instance;
    }

    private void loadProps(){
        props = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
            props.load(inputStream);
        }catch (Exception e){
            logger.error("加载application.properties文件发生异常",e);
        }finally {
            if (null != inputStream){
                try {
                    inputStream.close();
                }catch (Exception e){
                    inputStream = null;
                }
            }
        }
    }

    public String getProperty(String key){
        return getProperty(key,null);
    }

    public String getProperty(String key,String defaultValue){
        if(StringUtils.isNotBlank(key)){
            return props.getProperty(key);
        }
        return defaultValue;
    }
}
